import { commonUsagesDataList, commonUsagesSave, commonUsagesDeleteById } from '@/api/backstage'
import { useOtherStoreWithOut } from '@/store/modules/other'
import { nextTick } from 'Vue'
const otherStore = useOtherStoreWithOut()
let restaurants = otherStore.getContentData || []
let isLoading = false

const getContentListData = async () => {
  const contentType = otherStore.getContentType
  isLoading = true
  const res = await commonUsagesDataList({
    contentType: contentType,
    projectId: otherStore.getProjectId
  })
  if (res) {
    const data =
      (res.data &&
        res.data.map((item) => {
          return {
            value: item.content,
            ...item
          }
        })) ||
      []
    otherStore.setContentData(data)
    restaurants = data
  } else {
    restaurants = []
    otherStore.setContentData([])
  }
  isLoading = false
}

const querySearchAsync = async (queryString: string, cb: (arg: any) => void) => {
  if (isLoading) {
    setTimeout(() => {
      queryFn(queryString, cb)
    }, 200)
  } else {
    queryFn(queryString, cb)
  }
}
const queryFn = (queryString: string, cb: (arg: any) => void) => {
  const data = otherStore.getContentData
  if (queryString === null || queryString === undefined) {
    cb(data)
  } else {
    const results = queryString
      ? data.filter((item: any) => {
          return item.value && item.value.toLowerCase().indexOf(queryString.toLowerCase()) != -1
        })
      : data

    cb(results)
  }
}
/** 
 * @param contentType 快捷存储的类型，例如：
        省份 (1) 
        高速公路编号 (2)       
        路段类型 (3)     
        职务岗位(5)        
        角色类型(6)        
        告警分组(8)        
        计量单位(11)        
        设备所在科室(12)        
        所属业务系统(13)        
        机电设备类型(14)        
        设备标签(15)        
        操作系统(16)        
        设备连接协议(17)        
        分辨率(18)        
        所连接NVR(19)        
        连接交换机(20)        
        维护单位(21)        
        设施大类(22)        
        设施大类(23)        
        DNS(31)        
 */
const setContentType = async (contentType: string | number) => {
  const oldContentType = otherStore.getContentType
  if (oldContentType === contentType) return
  otherStore.setContentType(contentType)
  if (oldContentType !== contentType) {
    restaurants = []
    await getContentListData()
  }
}

/** 保存快捷存储，已存在的就不添加了 */
const saveCommonUsage = async (content) => {
  const isHave = restaurants.find((item: any) => item.content === content)
  if (!isHave) {
    const res = await commonUsagesSave({
      projectId: otherStore.getProjectId,
      content: content,
      contentType: otherStore.getContentType
    })
    if (res) {
      otherStore.setContentData([...restaurants, res!.data])
      await getContentListData()
    }
  }
}
const delCommonUsage = async (id: string) => {
  if (!id) return
  const index = restaurants.findIndex((item: any) => item.id === id)
  const res = await commonUsagesDeleteById({
    id: id
  })
  if (res) {
    restaurants.splice(index, 1)
    getContentListData()
  }
  return false
}

const handleSelect = (item: any) => {
  console.log('handleSelect', item)
}

export {
  querySearchAsync,
  handleSelect,
  setContentType,
  saveCommonUsage,
  delCommonUsage,
  restaurants
}
